

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 参考资料 视频资料 一、Shell概述 Shell是一个命令行解释器，用于接收应用程序或者用户命令，然后调用操作系统内核。Shell还是一个功能强大的编程语言，易编写、易调试、灵活性强。 二、解析器 使用命令查看系统所含的所有解析器 1cat &#x2F;etc&#x2F;shells  查看结果：  查看系统默认的解析器：（笔者是CentOS7系统） 1echo $SHELL   三、hello, w">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux _ Shell笔记1 Shell概述 _ Linux系统自带的解析器_bin_bash _ bash_sh执行脚本 _ 脚本中$n 、$#等特殊变量 _ condition条件判断">
<meta property="og:url" content="http://example.com/2022/01/13/Linux%20_%20Shell%E7%AC%94%E8%AE%B01%20Shell%E6%A6%82%E8%BF%B0%20_%20Linux%E7%B3%BB%E7%BB%9F%E8%87%AA%E5%B8%A6%E7%9A%84%E8%A7%A3%E6%9E%90%E5%99%A8_bin_bash%20_%20bash_sh%E6%89%A7%E8%A1%8C%E8%84%9A%E6%9C%AC%20_%20%E8%84%9A%E6%9C%AC%E4%B8%AD$n%20%E3%80%81$#%E7%AD%89%E7%89%B9%E6%AE%8A%E5%8F%98%E9%87%8F%20_%20condition%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 参考资料 视频资料 一、Shell概述 Shell是一个命令行解释器，用于接收应用程序或者用户命令，然后调用操作系统内核。Shell还是一个功能强大的编程语言，易编写、易调试、灵活性强。 二、解析器 使用命令查看系统所含的所有解析器 1cat &#x2F;etc&#x2F;shells  查看结果：  查看系统默认的解析器：（笔者是CentOS7系统） 1echo $SHELL   三、hello, w">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d3eb0c5a0c33445b878b6c8852d064e2.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/7f11fa96f4c44104bc024c6a0676c030.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ae00cf3253504d97a15f502f938ad576.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/1cfa99aa350e48f8aa689ceb4417710d.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/044a3507dce74eb189940b598242eb21.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fb5938d2e4064abf8a96e6bd781b84c8.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c5c6ad506dee4759aa441031c5f2f9e8.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/cfbc59ff661b4171bef685771bd6e78c.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2e588d552d924ef696c10ac1dd9bc903.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/967e686b5c65417aa6fa7ba1ed71bc03.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/db8cf7d21b6247e8a61f533820cb466f.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/04fa339ca487485fba74051ce1d52f1a.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ed0009d0653744178d0d02c06a02d9b8.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/f046b42058c94b65af5298ce4b4f9951.png">
<meta property="article:published_time" content="2022-01-13T11:47:47.000Z">
<meta property="article:modified_time" content="2022-08-22T15:45:55.855Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/d3eb0c5a0c33445b878b6c8852d064e2.png">
  
  
  
  <title>Linux _ Shell笔记1 Shell概述 _ Linux系统自带的解析器_bin_bash _ bash_sh执行脚本 _ 脚本中$n 、$#等特殊变量 _ condition条件判断 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Linux _ Shell笔记1 Shell概述 _ Linux系统自带的解析器_bin_bash _ bash_sh执行脚本 _ 脚本中$n 、$#等特殊变量 _ condition条件判断"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-01-13 19:47" pubdate>
          2022年1月13日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          2.9k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          25 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Linux _ Shell笔记1 Shell概述 _ Linux系统自带的解析器_bin_bash _ bash_sh执行脚本 _ 脚本中$n 、$#等特殊变量 _ condition条件判断</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><hr>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1hW41167NW?p=1">视频资料</a></p>
<h1 id="一、Shell概述"><a href="#一、Shell概述" class="headerlink" title="一、Shell概述"></a>一、Shell概述</h1><hr>
<p>Shell是一个命令行解释器，用于接收应用程序或者用户命令，然后调用操作系统内核。Shell还是一个功能强大的编程语言，易编写、易调试、灵活性强。</p>
<h1 id="二、解析器"><a href="#二、解析器" class="headerlink" title="二、解析器"></a>二、解析器</h1><hr>
<p>使用命令查看系统所含的所有解析器</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">cat /etc/shells<br></code></pre></td></tr></table></figure>

<p>查看结果：</p>
<p><img src="https://img-blog.csdnimg.cn/d3eb0c5a0c33445b878b6c8852d064e2.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>查看系统默认的解析器：（笔者是CentOS7系统）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">echo $SHELL<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/7f11fa96f4c44104bc024c6a0676c030.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="三、hello-world"><a href="#三、hello-world" class="headerlink" title="三、hello, world!"></a>三、hello, world!</h1><hr>
<p>使用vi命令打开一个（没有则创建）sh文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">vi hello.sh<br></code></pre></td></tr></table></figure>

<p>在文件中输入以下内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/bash</span><br>echo &quot;hello, wrold!&quot;<br></code></pre></td></tr></table></figure>

<p>执行sh脚本</p>
<p><strong>方式一：</strong> 使用sh或bash命令执行sh脚本</p>
<ul>
<li>sh 命令</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/ae00cf3253504d97a15f502f938ad576.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>bash命令</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/1cfa99aa350e48f8aa689ceb4417710d.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><strong>方式二：</strong> 赋予文件可执行的权限（同样能以两种路径方式访问）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs shell">chmod 777 hello.sh<br><span class="hljs-meta prompt_"># </span><span class="language-bash">相对路径</span><br>./hello.sh<br><span class="hljs-meta prompt_"># </span><span class="language-bash">绝对路径</span><br>/home/s0125/hello.sh<br></code></pre></td></tr></table></figure>

<h1 id="四、echo切换输出流"><a href="#四、echo切换输出流" class="headerlink" title="四、echo切换输出流"></a>四、echo切换输出流</h1><hr>
<p>echo命令默认是输出到控制台，可以通过 <code>&gt;&gt;</code> 符号来指定输出的位置</p>
<p>比如，将一个字符串内容输出到指定的文本当中</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">vi batch.sh<br></code></pre></td></tr></table></figure>

<p>脚本内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/bash</span><br>cd /home/s0125<br>touch love.txt<br>echo &quot;Uni likes girl.&quot; &gt;&gt; love.txt<br></code></pre></td></tr></table></figure>

<p>然后可以选择之前的两种方式执行，sh&#x2F;bash或者设置为可执行文件</p>
<p>这里笔者使用sh执行脚本</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">sh batch.sh<br><br>cat /home/s0125/love.txt<br></code></pre></td></tr></table></figure>

<p>运行结果：</p>
<p><img src="https://img-blog.csdnimg.cn/044a3507dce74eb189940b598242eb21.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>执行两次脚本后，结果出现了重复，说明 &gt;&gt; 默认是以追加方式输出的。</p>
<h1 id="五、Shell中的变量"><a href="#五、Shell中的变量" class="headerlink" title="五、Shell中的变量"></a>五、Shell中的变量</h1><hr>
<h2 id="5-1-系统变量"><a href="#5-1-系统变量" class="headerlink" title="5.1 系统变量"></a>5.1 系统变量</h2><p>常用系统变量</p>
<p><code>$HOME、$PWD、$SHELL、$USER</code>等</p>
<p><img src="https://img-blog.csdnimg.cn/fb5938d2e4064abf8a96e6bd781b84c8.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>显示当前Shell中所有变量：<code>set</code> 命令 ，这里grep的作用是过滤，从set结果里过滤出含( ) 的信息</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">set | grep \(\)<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/c5c6ad506dee4759aa441031c5f2f9e8.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="5-2-自定义变量"><a href="#5-2-自定义变量" class="headerlink" title="5.2 自定义变量"></a>5.2 自定义变量</h2><ol>
<li>定义语法<ul>
<li>定义变量：变量名&#x3D;值</li>
<li>撤销变量：unset 变量名</li>
<li>声明静态变量：readonly 变量，注意；无法 unset</li>
</ul>
</li>
<li>变量定义规则<ul>
<li>变量名称可以由字母、数字和下划线组成，但不能以数字开头，而且环境变量名建议大写。</li>
<li>等号两侧不能存在空格。</li>
<li>在bash中，变量默认类型都是字符串类型，无法直接进行数值运算。</li>
<li>变量的值如果有空格，需要使用双引号或单引号括起来</li>
</ul>
</li>
</ol>
<p>案例：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_"># </span><span class="language-bash">1. 定义变量A</span><br>[s0125@hadoop ~]$ A=5<br>[s0125@hadoop ~]$ echo $A<br>5<br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">2. 给变量A重新赋值</span><br>[s0125@hadoop ~]$ A=10<br>[s0125@hadoop ~]$ echo $A<br>10<br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">3. 撤销变量A</span><br>[s0125@hadoop ~]$ unset A<br>[s0125@hadoop ~]$ echo $A<br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">4. 声明静态的变量B=2， 不能<span class="hljs-built_in">unset</span></span><br>[s0125@hadoop ~]$ readonly B=2<br>[s0125@hadoop ~]$ echo $B<br>2<br>[s0125@hadoop ~]$ B=3<br>bash: B: 只读变量<br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">5. 在bash中，变量默认类型为字符串型，无法直接进行计算</span><br>[s0125@hadoop ~]$ C=1+2<br>[s0125@hadoop ~]$ echo $C<br>1+2<br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">6.变量的值如果有空格，需要用双引号或单引号括起来</span><br>[s0125@hadoop ~]$ D= i love u<br>bash: i: 未找到命令...<br>[s0125@hadoop ~]$ D = &quot;i love u&quot;<br>bash: D: 未找到命令...<br>[s0125@hadoop ~]$ D=&quot;i love u&quot;<br>[s0125@hadoop ~]$ echo $D<br>i love u<br></code></pre></td></tr></table></figure>

<p><code>export</code> 命令可以把变量提升为全局环境变量，可供其他 <strong>Shell</strong> 程序使用。</p>
<h2 id="5-3-特殊变量-n-接收脚本参数"><a href="#5-3-特殊变量-n-接收脚本参数" class="headerlink" title="5.3 特殊变量 $n 接收脚本参数"></a>5.3 特殊变量 <code>$n</code> 接收脚本参数</h2><ol>
<li>基本语法<ul>
<li><code>$n</code> ，其中的n为数字，<code>$0</code>则表示该脚本的名称，<code>$1-$9</code>代表第一到第九个参数，十以上的参数要用大括号包含，比如<code>$&#123;10&#125;</code></li>
</ul>
</li>
<li>案例</li>
</ol>
<p>创建并编辑一个脚本，它可以输出脚本的名称和传入的两个参数</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">vi uni.sh<br></code></pre></td></tr></table></figure>

<p>脚本内容如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/bash</span><br>echo &quot;脚本名称:$0 输入的两个参数: $1,$2&quot;<br></code></pre></td></tr></table></figure>

<p>执行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">sh uni.sh 参数1 参数2<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/cfbc59ff661b4171bef685771bd6e78c.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="5-4-特殊变量-获取参数个数"><a href="#5-4-特殊变量-获取参数个数" class="headerlink" title="5.4 特殊变量 $# 获取参数个数"></a>5.4 特殊变量 <code>$#</code> 获取参数个数</h2><ol>
<li>基本语法<ul>
<li>$# ，用于获取所有输入参数的个数，常用于循环</li>
</ul>
</li>
<li>案例</li>
</ol>
<p>获取输入参数的个数并输出</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/bash</span><br>echo &quot;参数个数: $#&quot;<br></code></pre></td></tr></table></figure>

<p>运行结果：</p>
<p><img src="https://img-blog.csdnimg.cn/2e588d552d924ef696c10ac1dd9bc903.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="5-5-特殊变量-、"><a href="#5-5-特殊变量-、" class="headerlink" title="5.5 特殊变量 $* 、$@"></a>5.5 特殊变量 <code>$*</code> 、<code>$@</code></h2><ol>
<li>基本语法<ul>
<li><code>$*</code>，代表命令行中所有的参数，<code>$*</code> 把所有的参数当成一个整体</li>
<li><code>$@</code>，代表命令行中所有参数，不过<code>$@</code>是把每个参数区分对待</li>
</ul>
</li>
</ol>
<h2 id="5-6-特殊变量"><a href="#5-6-特殊变量" class="headerlink" title="5.6 特殊变量 $?"></a>5.6 特殊变量 <code>$?</code></h2><ol>
<li>基本语法<ul>
<li><code>$?</code> 最后依次执行的命令返回的状态，如果正常执行则返回0，若为非0数（由脚本决定）则执行失败。</li>
</ul>
</li>
<li>操作</li>
</ol>
<p>输出脚本的一次执行状态</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/bash</span><br>echo $?<br></code></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs shell">[s0125@hadoop ~]$ vi uni.sh<br>[s0125@hadoop ~]$ sh uni.sh<br>0<br>[s0125@hadoop ~]$ <br></code></pre></td></tr></table></figure>

<h1 id="六、运算符"><a href="#六、运算符" class="headerlink" title="六、运算符"></a>六、运算符</h1><hr>
<ol>
<li>基本语法<ul>
<li><code>$((运算式))</code> 或者 <code>$[运算式]</code></li>
<li><code>expr +,-,\*, /,%</code> 加减乘除取余，注：expr运算符之间要有空格</li>
</ul>
</li>
<li>案例</li>
</ol>
<p>多种方式计算 (5 + 5) * 100 &#x2F; 200  + 20 的结果</p>
<p><img src="https://img-blog.csdnimg.cn/967e686b5c65417aa6fa7ba1ed71bc03.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="七、-条件判断"><a href="#七、-条件判断" class="headerlink" title="七、 条件判断"></a>七、 条件判断</h1><hr>
<ol>
<li><p>基本语法</p>
<ul>
<li><code>[ condition ] </code>，<code>confition</code> 前后有空格</li>
</ul>
<p>注意：条件非空即为 true，比如 [ uni ] 则返回true，[ ] 返回false</p>
</li>
<li><p>常用判断条件</p>
</li>
</ol>
<ul>
<li><p>两个整数之间比较</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><code>= </code></td>
<td>字符串比较</td>
</tr>
<tr>
<td><code>-lt </code></td>
<td>less than，表示 &lt; 小于</td>
</tr>
<tr>
<td><code>-le</code></td>
<td>less equal，表示 &lt;&#x3D; 小于等于</td>
</tr>
<tr>
<td><code>-eq</code></td>
<td>equal，表示 &#x3D;&#x3D;等于</td>
</tr>
<tr>
<td><code> -gt</code></td>
<td>greater than，表示 &gt;大于</td>
</tr>
<tr>
<td><code>-ge</code></td>
<td>greater equal，表示&gt;&#x3D;大于等于</td>
</tr>
<tr>
<td><code>-ne</code></td>
<td>Not equal，表示!&#x3D; 不等于</td>
</tr>
</tbody></table>
</li>
<li><p>按照文件权限进行判断</p>
</li>
</ul>
<table>
<thead>
<tr>
<th>符号</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><code>-r</code></td>
<td>read 有读的权限</td>
</tr>
<tr>
<td><code>-w</code></td>
<td>write 有写的权限</td>
</tr>
<tr>
<td><code>-x</code></td>
<td>execute 有执行的权限</td>
</tr>
</tbody></table>
<ul>
<li>按照文件类型进行判断</li>
</ul>
<table>
<thead>
<tr>
<th>符号</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><code>-f</code></td>
<td>文件存在并且是一个常规的文件file</td>
</tr>
<tr>
<td><code>-e</code></td>
<td>文件存在（existence）</td>
</tr>
<tr>
<td><code>-d</code></td>
<td>文件存在并且是一个目录 （directory）</td>
</tr>
</tbody></table>
<ol start="2">
<li>案例</li>
</ol>
<ul>
<li>比较输入的第一个参数是否大于第二个参数，用echo “$?” 输出结果</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell">[ 1 -gt 2]<br>echo $?<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/db8cf7d21b6247e8a61f533820cb466f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>判断脚本uni.sh是否有写权限</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell">[ -w uni.sh ]<br>echo &quot;$?&quot;<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/04fa339ca487485fba74051ce1d52f1a.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>判断文件是否存在</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell">[ -e /home/s0125/uni.sh ]<br>echo $?<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/ed0009d0653744178d0d02c06a02d9b8.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>多条件判断 （&amp;&amp;， 表示前一条命令执行成功时，才会执行后面的命令， || 表示上一条命令执行失败后，才执行下一条命令）</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">[ 1 -gt 2 ] &amp;&amp; echo true || echo false<br><br>[ 2 -gt 1 ] &amp;&amp; echo true || echo false<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/f046b42058c94b65af5298ce4b4f9951.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Linux/" class="category-chain-item">Linux</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Linux/">#Linux</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Linux _ Shell笔记1 Shell概述 _ Linux系统自带的解析器_bin_bash _ bash_sh执行脚本 _ 脚本中$n 、$#等特殊变量 _ condition条件判断</div>
      <div>http://example.com/2022/01/13/Linux _ Shell笔记1 Shell概述 _ Linux系统自带的解析器_bin_bash _ bash_sh执行脚本 _ 脚本中$n 、$#等特殊变量 _ condition条件判断/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年1月13日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/01/14/MyBatis%20%E7%AC%94%E8%AE%B02%20SQL%E6%98%A0%E5%B0%84XML%E6%96%87%E4%BB%B6%E6%A0%87%E7%AD%BE%20select%E3%80%81insert%E3%80%81update%20_%20XML%E8%8E%B7%E5%8F%96%E5%A4%9A%E4%B8%AA%E5%8F%82%E6%95%B0%20_%20MyBatis%E5%AE%9E%E7%8E%B0%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%20_%20resultMap%E6%A0%87%E7%AD%BE%E6%98%A0%E5%B0%84/" title="MyBatis 笔记2 SQL映射XML文件标签 select、insert、update _ XML获取多个参数 _ MyBatis实现多表查询 _ resultMap标签映射">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">MyBatis 笔记2 SQL映射XML文件标签 select、insert、update _ XML获取多个参数 _ MyBatis实现多表查询 _ resultMap标签映射</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/13/MyBatis%20%E7%AC%94%E8%AE%B01%20%E5%9F%BA%E4%BA%8EMaven%E6%90%AD%E5%BB%BAMyBatis%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%20_%20Mapper%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89%E4%BB%A5%E5%8F%8AXML%E6%96%B9%E5%BC%8F%E6%B3%A8%E5%85%A5%20_%20%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93%20_%20SqlSession%E5%88%9B%E5%BB%BA%E6%96%B9%E5%BC%8F%20_%20%E5%9F%BA%E4%BA%8E%E6%A1%86%E6%9E%B6%E5%AE%9E%E7%8E%B0%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BA%A4%E4%BA%92/" title="MyBatis 笔记1 基于Maven搭建MyBatis开发环境 _ Mapper接口定义以及XML方式注入 _ 配置文件使用总结 _ SqlSession创建方式 _ 基于框架实现数据库交互">
                        <span class="hidden-mobile">MyBatis 笔记1 基于Maven搭建MyBatis开发环境 _ Mapper接口定义以及XML方式注入 _ 配置文件使用总结 _ SqlSession创建方式 _ 基于框架实现数据库交互</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
